﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    
    

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        
    }
    protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }
    protected void DropDownList6_SelectedIndexChanged(object sender, EventArgs e)
    {
       
        
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        int ArrCount = 0;
        String dp5 = DropDownList5.SelectedValue.ToString();
        string MyString5 = dp5;
        MyString5 = MyString5.Remove(2, 1);

        String dp6 = DropDownList6.SelectedValue.ToString();
        string MyString6 = dp6;
        MyString6 = MyString6.Remove(2, 1);

        int dpint5 = int.Parse(MyString5);
        int dpint6 = int.Parse(MyString6);

        if (dpint5 > dpint6)
        {
            Label1.Text = "You have selected wrong time period";
            DropDownList5.SelectedIndex = 0;
            DropDownList6.SelectedIndex = 0;

        }
        else
        {

            String selectedStartTime = DropDownList5.SelectedValue;
            String selectedEndTime = DropDownList6.SelectedValue;
            String selectedDate = Calendar1.SelectedDate.ToShortDateString();

            DateTime Sdate = Convert.ToDateTime(selectedDate);
            
            DateTime Stime1 = Convert.ToDateTime(selectedStartTime);            
            TimeSpan myt1 = Stime1.TimeOfDay;
            String Strinmyt1 = myt1.ToString();
            String ConStrinmyt1 = Strinmyt1.Replace(":", ".");
            ConStrinmyt1 = ConStrinmyt1.Substring(0,5);
            float Uset1 = Convert.ToSingle(ConStrinmyt1);
            
            DateTime Stime2 = Convert.ToDateTime(selectedEndTime);            
            TimeSpan myt2 = Stime2.TimeOfDay;
            String Strinmyt2 = myt2.ToString();
            String ConStrinmyt2 = Strinmyt2.Replace(":", ".");
            ConStrinmyt2 = ConStrinmyt2.Substring(0, 5);
            float Uset2 = Convert.ToSingle(ConStrinmyt2);

            Session["mySesD"] = Sdate;
            Session["mySes1"] = myt1;
            Session["mySes2"] = myt2;

            String ConString = ConfigurationManager.ConnectionStrings["myDBConnection"].ConnectionString;
            using (SqlConnection connection = new SqlConnection(ConString))
            {
                connection.Open();
                string GetDepTimes = "select eDateTime,bookSlotNo from booking where sDate = '" + selectedDate + "'";
                SqlCommand cmd3 = new SqlCommand(GetDepTimes, connection);
                using (SqlDataReader rdr = cmd3.ExecuteReader())
                {
                    int i = 0;
                    string[] myArr1 = new string[100];

                    while (rdr.Read())
                    {
                        string gotrow1 = Convert.ToString(rdr["eDateTime"]);
                        string gotrow2 = Convert.ToString(rdr["bookSlotNo"]);
                        string conrow1 = string.Concat(gotrow1, gotrow2);

                        myArr1[i] = conrow1;
                        i++;
                        ArrCount++;
                    }
                    Session["passArr1"] = myArr1;
                }

                connection.Close();


                connection.Open();
                string GetArrTimes = "select sDateTime from booking where sDate = '" + selectedDate + "'";
                SqlCommand cmd4 = new SqlCommand(GetArrTimes, connection);
                using (SqlDataReader rdr2 = cmd4.ExecuteReader())
                {
                    int j = 0;
                    string[] myArr2 = new string[100];
                    while (rdr2.Read())
                    {
                        string gotrow2 = Convert.ToString(rdr2["sDateTime"]);
                        myArr2[j] = gotrow2;
                        j++;
                    }
                    Session["passArr2"] = myArr2;
                }
                connection.Close();

            }

            string[] gtArr1 = (string[])Session["passArr1"];
            string[] gtArr2 = (string[])Session["passArr2"];


            if (String.IsNullOrEmpty(gtArr1[0]))
            {
                String ConString2 = ConfigurationManager.ConnectionStrings["myDBConnection"].ConnectionString;
                using (SqlConnection connection1_1 = new SqlConnection(ConString2))
                {
                    connection1_1.Open();
                    SqlCommand command = new SqlCommand("updateAlltoAvailable", connection1_1);
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.ExecuteNonQuery();
                    connection1_1.Close();
                    //All the slots should indicate as available
                }

            }
            else
            {
                String ConString2 = ConfigurationManager.ConnectionStrings["myDBConnection"].ConnectionString;
                using (SqlConnection connection1_2 = new SqlConnection(ConString2))
                {
                    connection1_2.Open();
                    SqlCommand command = new SqlCommand("updateAlltoAvailable", connection1_2);
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.ExecuteNonQuery();
                    connection1_2.Close();
                    //All the slots should indicate as available for reset
                }
                
                //foreach (string value in gtArr1)
                for (int index = 0; index < ArrCount;index++ )
                {

                    string newArrs1 = gtArr1[index].Substring(0, 5);
                    newArrs1 = newArrs1.Replace(":", ".");
                    float IntDepArr = Convert.ToSingle(newArrs1);
                    //departure times

                    string newArrs2 = gtArr2[index].Substring(0, 5);
                    newArrs2 = newArrs2.Replace(":", ".");
                    float IntArrArr = Convert.ToSingle(newArrs2);
                    //arrival times


                    if ((Uset1 >= IntArrArr && Uset1 <= IntDepArr) || (Uset2 >= IntArrArr && Uset2 <= IntDepArr) || (Uset1 <= IntArrArr && Uset2 >= IntDepArr))
                    {
                        string slotfrmArr = gtArr1[index];
                        slotfrmArr = slotfrmArr.Substring(8);
                        int IntSlot = Convert.ToInt16(slotfrmArr);

                        String ConString2_1 = ConfigurationManager.ConnectionStrings["myDBConnection"].ConnectionString;
                        using (SqlConnection connection2 = new SqlConnection(ConString2_1))
                        {
                            connection2.Open();
                            SqlCommand command = new SqlCommand("updateStatustableRed", connection2);
                            command.CommandType = System.Data.CommandType.StoredProcedure;
                            command.Parameters.AddWithValue("@mySlot2", IntSlot);
                            command.ExecuteNonQuery();
                            connection2.Close();
                            //this data should store to the status table as red
                        }
                    }
                    else
                    {
                        string slotfrmArr = gtArr1[index];
                        slotfrmArr = gtArr1[index].Substring(8);
                        int IntSlot2 = Convert.ToInt16(slotfrmArr);

                        String ConString3 = ConfigurationManager.ConnectionStrings["myDBConnection"].ConnectionString;
                        using (SqlConnection connection3 = new SqlConnection(ConString3))
                        {
                            connection3.Open();
                            SqlCommand command = new SqlCommand("updateStatustableGreen", connection3);
                            command.CommandType = System.Data.CommandType.StoredProcedure;

                            command.Parameters.AddWithValue("@mySlot", IntSlot2);

                            command.ExecuteNonQuery();

                            connection3.Close();
                            //this data should store to the status table as green
                        }
                    }

                    //index++;
                }
            }



            Response.Redirect("Selectslots.aspx");

        }
    }
}